// JavaScript Document
function getPreporuke(kom_id){
	if(kom_id>0){
		LoadRSDoc("preporuke.php?a=getKomitentPreporuke&id="+kom_id,"afterGetPreporuke");
	}
}
function afterGetPreporuke(prep){
	if(prep.match(/{!}/)){	// ima preporuka
	
		var rows = prep.split(/{!!}/);
		
		for(var i=0; i<rows.length; i++){
			var flds = rows[i].split(/{!}/);
			var delid = flds[0];
			var html = flds[1];
			
			var ph = document.getElementById("preporuke_holder_"+delid)
			if(ph){
				ph.style.display="block";
				var div = document.createElement("div");
				div.className = "preporukaHTML";
				div.style.display="none";
				div.innerHTML = html;
				ph.appendChild(div);
			}
		}
	}else{					// nema preporuka
	}
}

function togglePreporuke(btn){
	var pd=btn.nextSibling.nextSibling; 
	if(pd){
		if(pd.style.display=='block'){
			pd.style.display='none';
		}else{
			pd.style.display='block';
			var h = pd.offsetHeight;
			pd.style.marginTop = (-1*Math.round(h/2))+12+"px";
		}
	}else{
		alert("PD is "+pd);
	}
}
function switchKomentari(btn){
	var ul = btn.nextSibling.nextSibling.nextSibling;
	if(ul){
		var lis = ul.getElementsByTagName("li")
		var listyle="";
		if(ul.style.display=="block"){
			ul.style.display="none";
			listyle="none";
		}else{
			ul.style.display="block";
			listyle="block";
		}
		for(var i=0; i<lis.length; i++){
			lis[i].style.display=listyle;
		}
	}
}
function preporukeAdminShowPreps(komid){
	var ul = document.getElementById("preporukaAdminPrepList");
	var lis = ul.getElementsByTagName("li");
	
	for(var i=1; i<lis.length; i++){
		
			var liid = lis[i].getAttribute("komid");
			if(liid==komid){
				lis[i].style.display="block";
			}else{
				lis[i].style.display="none";
			}
		
	}
}
function preporukaEdit(btn,prepID,delID){
	var li = btn.parentNode.parentNode;
	var inputs = li.getElementsByTagName("input");
	var hidden=Array();
	for(var i=0; i<inputs.length; i++){
		if(inputs[i].type == "hidden"){
			hidden[inputs[i].name] = inputs[i].value
		}
	}
	
	var h1 = li.getElementsByTagName("h1");
	h1 = h1[0].innerHTML.split(/<span /,2);
	var title = h1[0];
	
	var p = li.getElementsByTagName("p");
	var txt=sign=mesto=adresa=email=tel="";
	for(var i=0; i<p.length; i++){
		switch(p[i].className){
			case "prepText":
				txt = p[i].innerHTML;
				break;
			case "prepSign":
				sign = p[i].innerHTML;
				break;
			case "prepMesto":
				mesto = p[i].innerHTML;
				break;
			case "prepAdresa":
				adresa = p[i].innerHTML;
				break;
			case "prepEmail":
				email = p[i].innerHTML;
				break;
			case "prepTel":
				tel = p[i].innerHTML;
				break;
		}
	}
	
	var tf = document.getElementById("preporukaEdit");
	if(tf){
		var ke = document.getElementById("commentEdit");
		if(ke.style.display=="block"){
			ke.style.display="none";
		}
		var titleHead = document.getElementById("prepEditTitle");
		//document.getElementById("preporukaNewFormFields").style.display="none";
		titleHead.innerHTML = "Izmena preporuke";
		titleHead.className = "izmena";
		//var templateForm = tf.cloneNode(true);
		
		var templateForm = tf;
		var komid	= trim(li.getAttribute("komid"));
		var komname	=trim(li.getAttribute("komname"));
		
		var els = tf.elements;
		els["komitent_edit"].value=komname;
		els["komitent_edit_id"].value=komid;
		els["naslov_edit"].value = title;
		
		txt = txt.replace(/&amp;/g,"&");
		txt = txt.replace(/<br(\s.*?)*\/*>/g,"\n");
		els["text_edit"].value = txt;
		
		els["prep_id_edit"].value = prepID;
		els["new"].value = 0;
		
		els["edit_ime"].value = sign;
		els["edit_mesto"].value = mesto;
		els["edit_adresa"].value = adresa;
		els["edit_email"].value = email;
		els["edit_telefon"].value = tel;
		els["kontrola"].checked = (hidden["kontrola"]==1)?true:false;
		//els["skBtn"].style.display = "none";
		
		var komdels = hidden["komdels"].split(/;/);
		for(i=0; i<els["delatnost_edit"].options.length; i++){
			var opt = els["delatnost_edit"].options[i]
			if(opt.value == delID){
				opt.selected = true;
			}else{
				opt.selected = false;
			}
			
			if(komdels.indexOf(opt.value)>=0){
				opt.disabled = false;
				opt.style.display = "block";
			}else{
				opt.disabled = true;
				opt.style.display = "none";
			}
		}
		for(i=0; i<els["tip_edit"].options.length; i++){
			var opt = els["tip_edit"].options[i]
			if(opt.value == hidden["tip_preporuke"]){
				opt.selected = true;
			}else{
				opt.selected = false;
			}
		}
		for(i=0; i<els["tema_edit"].options.length; i++){
			var opt = els["tema_edit"].options[i]
			if(opt.value == hidden["tema_preporuke"]){
				opt.selected = true;
			}else{
				opt.selected = false;
			}
		}
		templateForm.style.top = (btn.offsetTop+btn.offsetHeight)+"px";
		templateForm.style.left = (btn.parentNode.parentNode.offsetLeft)+"px";
		templateForm.style.display = "block";
	}
}
function preporukaNew(btn,komid){
	var tf = document.getElementById("preporukaEdit");
	if(tf){
		var ke = document.getElementById("commentEdit");
		if(ke.style.display=="block"){
			ke.style.display="none";
		}
		
		var titleHead = document.getElementById("prepEditTitle");
		//document.getElementById("preporukaNewFormFields").style.display="block";
		titleHead.innerHTML = "Nova preporuka";
		titleHead.className = "nova";
		
		var els = tf.elements;
		els["komitent_edit"].value="";
		els["komitent_edit_id"].value="";
		els["skBtn"].style.display = "block";
		els["naslov_edit"].value = "";
		
		els["delatnost_edit"].selectedIndex = 0;
		for(var i=0; i<els["delatnost_edit"].options.length; i++){
			els["delatnost_edit"].options[i].disabled = true;
			els["delatnost_edit"].options[i].style.display = "none";
		}
		
		els["tip_edit"].selectedIndex = 0;
		els["tema_edit"].selectedIndex = 0;
		els["text_edit"].value = "";
		els["new"].value = 1;
		
		els["edit_ime"].value = "";
		els["edit_mesto"].value = "";
		els["edit_adresa"].value = "";
		els["edit_email"].value = "";
		els["edit_telefon"].value = "";
		
		
		tf.style.top = (btn.offsetTop+btn.offsetHeight)+"px";
		tf.style.left = (btn.parentNode.parentNode.offsetLeft)+"px";
		tf.style.display="block";
	}
}
function prepUpdateKomdels(resp,selID){
	var _delSEL = document.getElementById(selID);
	var delids = resp.split(/;/);
	for(var i=0; i<_delSEL.options.length; i++){
		var opt = _delSEL[i];
		if(delids.indexOf(opt.value)>=0){
			opt.style.display="block"
			opt.disabled = false;
		}else{
			opt.style.display="none"
			opt.disabled = true;
		}
	}
}
function afterSavePrep(response){
	var komid="";
	if(response.match(/\{!\}/)){
		response = response.split("{!}",2);
		komid = response[0];
		response = response[1];
	}
	alert(response);
	LoadRSDoc("preporuke.php?komitent="+komid,"setContent");
}

function commentEdit(btn){
	var ef = document.getElementById("commentEdit");
	if(ef){
		var pe = document.getElementById("preporukaEdit");
		if(pe.style.display=="block"){
			pe.style.display="none";
		}
		var titleHead = document.getElementById("commEditTitle");
		titleHead.innerHTML = "Izmena komentara";
		
		var dad = btn.parentNode.parentNode;
		var p = dad.getElementsByTagName("p");
		var strong = dad.getElementsByTagName("strong");
		
		var inpt = dad.getElementsByTagName("input");
		var hidden = Array();
		for(var i=0; i<inpt.length; i++){
			if(inpt[i].name && inpt[i].value){
				hidden[inpt[i].name] = inpt[i].value;
			}
		}
		
		var kom_naslov = trim(strong[0].innerHTML);
		for(i=0; i<p.length; i++){
			switch(p[i].className){
				case "commText":
					var kom_text = trim(p[i].innerHTML);
					break;
				case "commSign":
					var kom_posiljalac = trim(p[i].innerHTML);
					break;
			}
		}
		
		var els = ef.elements
		var li = btn.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
		els["new"].value = 0
		els["edit_comment_id"].value = hidden["komentar_id"]
		els["edit_komitent_id"].value = li.getAttribute("komid");
		els["edit_commprep_id"].value = hidden["preporuka_id"]
		els["kom_edit_naslov"].value = kom_naslov
		els["kom_edit_text"].value = kom_text
		els["kom_edit_posiljalac"].value = kom_posiljalac
		
		if(parseInt(hidden["kontrola"])>0){
			els["kom_kontrola"].checked = true;
		}else{
			els["kom_kontrola"].checked = false;
		}
		
		els["kom_edit_mesto"].value = hidden["mesto"];
		els["kom_edit_adresa"].value = hidden["adresa"];
		els["kom_edit_email"].value = hidden["email"];
		els["kom_edit_tel"].value = hidden["tel"];
	
		ef.style.left = btn.parentNode.parentNode.offsetLeft+"px"
		ef.style.top = (btn.offsetTop+btn.offsetHeight)+"px"
		ef.style.display = "block";
	}
}
function commentNew(btn,prepid){
	var ef = document.getElementById("commentEdit");
	if(ef){
		var pe = document.getElementById("preporukaEdit");
		if(pe.style.display=="block"){
			pe.style.display="none";
		}
		var titleHead = document.getElementById("commEditTitle");
		titleHead.innerHTML = "Novi komentar";
		
		var els = ef.elements;
		
		var li = btn.parentNode.parentNode.parentNode;
		
		for(var i=0; i<els.length; i++){
			var nodename = els[i].nodeName.toLowerCase();
			if(nodename == "input"){
				var type = els[i].type.toLowerCase();
				if(type == "text"){
					els[i].value = "";
				}else if(type == "checkbox"){
					els[i].checked = false;
				}else if(type == "hidden"){
					switch(els[i].name){
						case "edit_komitent_id":
							els[i].value = li.getAttribute("komid");
							break;
						case "edit_commprep_id":
							els[i].value = prepid
							break;
					}
				}
			}else if(nodename == "textarea"){
				els[i].value = "";
			}
		}
		els["new"].value = "1";
		
		ef.style.left = btn.parentNode.parentNode.offsetLeft+"px"
		ef.style.top = (btn.offsetTop+btn.offsetHeight)+"px"
		ef.style.display = "block";
	}
}
function afterSaveComment(response){
	if(response.match(/\{!\}/)){
		response = response.split("{!}",2);
		komid = response[0];
		response = response[1];
	}
	alert(response);
	LoadRSDoc("preporuke.php?komitent="+komid,"setContent");
}

function afterEditDelPrep(resp,komid){
	alert(resp);
	LoadRSDoc("preporuke.php?komitent="+komid,"setContent");
}


var moving=false;
var diffX=diffY=0;
function startMoveIt(btn,e){
	
	diffX = btn.parentNode.style.left.length>0?parseInt(btn.parentNode.style.left):0;
	diffY = btn.parentNode.style.top.length>0?parseInt(btn.parentNode.style.top):0;
	diffX = e.pageX-diffX;
	diffY = e.pageY-diffY;
	window.status = diffX+"-"+diffY;
	
	moving=true;
	document.body.bttn = btn;
	document.body.onmousemove = moveIt;
}
function stopMoveIt(btn){
	moving=false;
	document.body.onmousemove = null;
}
function moveIt(e){
	if(moving){
		var l = (e.pageX-diffX);
		var t = (e.pageY-diffY);
		//window.status = e.pageX+"    "+l+"-"+t+"     "+diffX+"-"+diffY
		this.bttn.parentNode.style.left = l+"px";
		this.bttn.parentNode.style.top = t+"px";
	}
}